package com.yanqu.road.dao.impl.servercenter;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.servercenter.ChannelHotUpdateConfigVersion;
import com.yanqu.road.entity.servercenter.HotPackVersion;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 热更包版本信息
 */
public class HotPackVersionDaoImpl extends TempDao {

	public List<HotPackVersion> getHotPackVersionList() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_hot_package_version";
		List<HotPackVersion> dataList = new ArrayList<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				HotPackVersion data = new HotPackVersion();
				data.setPackageMark(rs.getString("packageMark"));
				data.setPackageVersion(rs.getString("packageVersion"));
				data.setHotPackVersion(rs.getString("hotPackVersion"));
				data.setReview(rs.getBoolean("isReview"));
				data.setRecommendVersion(rs.getBoolean("isRecommendVersion"));
				data.setWhiteRecommendVersion(rs.getBoolean("isWhiteRecommendVersion"));
				data.setUpdateLevel(rs.getInt("updateLevel"));
				data.setDelete(rs.getBoolean("isDelete"));
				data.setWhiteUpdateLevel(rs.getInt("whiteUpdateLevel"));
				data.setHotPackUpdateCheck(rs.getBoolean("hotPackUpdateCheck"));
				data.setForwardRecommendVersion(rs.getBoolean("isForwardRecommendVersion"));
				data.setForwardUpdateLevel(rs.getInt("forwardUpdateLevel"));
				data.setReviewAppIdList(StringUtils.stringToStringList(rs.getString("reviewAppIdList"), ";"));
				dataList.add(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataList;
	}

	private static String getChannelHotUpdateConfigVersionSql = "select * from `t_s_config_update_version` where `isDelete`=0";
	public Map<String, List<ChannelHotUpdateConfigVersion>> getChannelHotUpdateConfigVersionList() {
		Map<String, List<ChannelHotUpdateConfigVersion>> map = new HashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(getChannelHotUpdateConfigVersionSql);
			rs = call.executeQuery();
			while (rs.next()) {
				ChannelHotUpdateConfigVersion data = new ChannelHotUpdateConfigVersion();
				data.setChannelId(rs.getInt("channelId"));
				data.setPackageMark(rs.getString("packageMark"));
				data.setHotfixVersion(rs.getString("hotfixVersion"));
				data.setUpdateScope(rs.getString("updateScope"));
				data.setUpdateVersion(rs.getString("updateVersion"));
				if(!map.containsKey(data.getPackageMark())){
					map.put(data.getPackageMark(), new ArrayList<>());
				}
				map.get(data.getPackageMark()).add(data);
			}
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return map;
	}
}
